﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA;
using System.Data;
using Pixysoft.Debug;
using Pixysoft.Framework.Noebe.MemoryCaches.Core;
using Pixysoft.Framework.Configurations;
using System.Threading;


namespace Pixysoft.Framework.Noebe.MemoryCaches
{
    class tc_1000
    {
        private static string root = Pixysoft.StringFilter.RegularPath(
            AppDomain.CurrentDomain.BaseDirectory);
        // common test cache manager
        public void test011()
        {
            NoebeConfiguration configfrom = new NoebeConfiguration();
            configfrom.Filename = root + @"\citibox.info.db.mdb";
            configfrom.DatabaseType = DatabaseType.Access;
            NoebeManager.Reload(configfrom);
            CacheNoebeManager.Initialize();
            DateTime start = DateTime.Now;
            TimeSpan span = DateTime.Now - start;
            CacheNoebeManager.Instance.Setting.MaxCapacity = 7000;

            INoebeManager manager = CacheNoebeManager.Instance;

            INoebeCommand command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE = :ITEMTYPECODE";
            command.Parameters.Add("ITEMTYPECODE", "2001");
            start = DateTime.Now;
            DataTable table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE = :ITEMTYPECODE";
            command.Parameters.Add("ITEMTYPECODE", "2001");
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            CacheNoebeManager.Instance.ClearAll();
            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE = :ITEMTYPECODE";
            command.Parameters.Add("ITEMTYPECODE", "2001");
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE = :ITEMTYPECODE";
            command.Parameters.Add("ITEMTYPECODE", "2001");
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);
        }

        // 测试带保存的 cache manager
        public void test012()
        {
            NoebeConfiguration configfrom = new NoebeConfiguration();
            configfrom.Filename = root + @"\citibox.info.db.mdb";
            configfrom.DatabaseType = DatabaseType.Access;
            NoebeManager.Initialize(configfrom);
            CacheNoebeManager.Initialize();
            DateTime start = DateTime.Now;
            TimeSpan span = DateTime.Now - start;
            CacheNoebeManager.Instance.Setting.MaxCapacity = 13000;
            CacheNoebeManager.Instance.Setting.TimeSpan = -1;
            CacheNoebeManager.Instance.Setting.CacheMode = CacheMode.DeepCache;

            for (int i = 0; i < 10; i++)
            {
                INoebeManager manager = CacheNoebeManager.Instance;
                INoebeCommand command = manager.NoebeCommand;
                command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 2000" + i % 2;
                start = DateTime.Now;
                DataTable table = command.ExecuteReader();
                span = (DateTime.Now - start);
                Console.WriteLine(span.Milliseconds);
                Console.WriteLine(table.Rows.Count);

                INoebeManager manager2 = NoebeManager.Instance;
                INoebeCommand command2 = manager2.NoebeCommand;
                command2.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 2000" + i % 2;
                start = DateTime.Now;
                DataTable table2 = command2.ExecuteReader();
                span = (DateTime.Now - start);
                Console.WriteLine(span.Milliseconds);
                Console.WriteLine(table2.Rows.Count);

                Console.WriteLine();
            }
        }

        // 测试中途被修改的 cache manager
        public void test013()
        {
            NoebeConfiguration configfrom = new NoebeConfiguration();
            configfrom.Filename = root + @"\citibox.info.db.mdb";
            configfrom.DatabaseType = DatabaseType.Access;
            NoebeManager.Initialize(configfrom);
            CacheNoebeManager.Initialize();
            DateTime start = DateTime.Now;
            TimeSpan span = DateTime.Now - start;
            CacheNoebeManager.Instance.Setting.MaxCapacity = 7000;
            CacheNoebeManager.Instance.Setting.TimeSpan = -1;
            CacheNoebeManager.Instance.Setting.CacheMode = CacheMode.DeepCache;
            INoebeManager manager = CacheNoebeManager.Instance;

            INoebeCommand command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 27831";
            start = DateTime.Now;
            DataTable table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 27831";
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);


            command = manager.NoebeCommand;
            command.SQL = "UPDATE SYS_ITEMTYPECOUNTER SET ITEMTYPECODE = -1 WHERE ITEMTYPECODE > 50000";
            command.Execute();

            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 27831";
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 27831";
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);
        }

        // 测试 缓存过期
        public void test014()
        {
            NoebeConfiguration configfrom = new NoebeConfiguration();
            configfrom.Filename = root + @"\citibox.info.db.mdb";
            configfrom.DatabaseType = DatabaseType.Access;
            NoebeManager.Initialize(configfrom);
            CacheNoebeManager.Initialize();
            DateTime start = DateTime.Now;
            TimeSpan span = DateTime.Now - start;
            CacheNoebeManager.Instance.Setting.MaxCapacity = 7000;
            CacheNoebeManager.Instance.Setting.TimeSpan = -1;
            CacheNoebeManager.Instance.Setting.CacheMode = CacheMode.DeepCache;
            INoebeManager manager = CacheNoebeManager.Instance;

            INoebeCommand command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 27831";
            start = DateTime.Now;
            DataTable table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            Thread.Sleep(1000);

            command = manager.NoebeCommand;
            command.SQL = "SELECT * FROM SYS_ITEMTYPECOUNTER WHERE ITEMTYPECODE > 27831";
            start = DateTime.Now;
            table = command.ExecuteReader();
            span = (DateTime.Now - start);
            Console.WriteLine(span.Milliseconds);
            Console.WriteLine(table.Rows.Count);

            //Thread.Sleep(1000);

            try
            {
                CacheNoebeManager.StateSave();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}
